package com.kunya.product.controller.portall;

import com.github.pagehelper.PageInfo;
import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.CommonUtil;
import com.kunya.product.po.Product;
import com.kunya.product.po.ProductCategory;
import com.kunya.product.service.category.IProductCategoryService;
import com.kunya.product.service.product.IProductCommentService;
import com.kunya.product.service.product.IProductService;
import com.kunya.product.vo.ProductDetailVo;
import com.kunya.product.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author zp
 * @Description: 商品controller
 * @date 2018/5/30 9:56
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductCategoryService iProductCategoryService;

    @Autowired

    private IProductCommentService iProductCommentService;


    /**
     * @author zp
     * @Description: 前台获取商品详情接口方法
     * @param
     * @date 2018/5/30 14:27
     */
    @RequestMapping("/detail")
    public ServerResponse<ProductDetailVo> detail(Long productNumber, HttpServletRequest httpServletRequest){
        return iProductService.getProductDetailVo(productNumber, CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/addnew")
    public ServerResponse addnew(Long[] categoryIds, Product product,HttpServletRequest request){
        return iProductService.saveProduct(categoryIds,product,CommonUtil.getTablePiefix(request));
    }

    @RequestMapping("/getCategories")
    public ServerResponse<List<ProductCategory>> getCategories(Long productId, HttpServletRequest request){
        return iProductCategoryService.getProductCatesByProductId(productId,CommonUtil.getTablePiefix(request));
    }

   /**
    * @author zp
    * @Description: 根据关键词 商品分类 动态排序 分页查询商品列表
    * @param
    * @date 2018/6/14 16:54
    */
   @RequestMapping("/getProductByKeywordCategory")
   public ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,
                                                                Long categoryId,
                                                                @RequestParam(value = "pageNum" ,required = false,defaultValue = "1")  int pageNum,
                                                                @RequestParam(value = "pageSize" ,required = false,defaultValue = "10")int pageSize,
                                                                String orderBy,
                                                                HttpServletRequest httpServletRequest){
        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy,CommonUtil.getTablePiefix(httpServletRequest));
    }

    /**
     * 修改商品
     * @param productVo
     * @return
     */
    @RequestMapping(value="/update_product",method=RequestMethod.POST)
    public ServerResponse updateProduct(ProductVo productVo){
       return iProductService.updateProduct(productVo,"340_");
    }

    /**
     * 下架商品
     * @param productId
     * @return
     */
    @RequestMapping(value = "/offline_product",method = RequestMethod.GET)
    public ServerResponse offlineProduct(Long productId){
        return iProductService.offlineProduct(productId,"340_");
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @RequestMapping(value="remove_product",method=RequestMethod.GET)
    public ServerResponse removeProduct(Long productId) throws IOException {
        return iProductService.removeProduct(productId,"340_");
    }
    @RequestMapping(value="/test",method=RequestMethod.GET)
    public ServerResponse test()  {
        return iProductCommentService.test();
    }

}
